package com.cmcm.su

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin
import org.aspectj.bridge.IMessage
import org.aspectj.bridge.MessageHandler
import org.aspectj.tools.ajc.Main
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile

class HugoPlugin implements Plugin<Project> {
    @Override void apply(Project project) {
        def hasApp = project.plugins.withType(AppPlugin)
        def hasLib = project.plugins.withType(LibraryPlugin)
        if (!hasApp && !hasLib) {
            throw new IllegalStateException("'android' or 'android-library' plugin required.")
        }


        final def log = project.logger
        log.error("Hugo start")
        final def variants
        if (hasApp) {
            variants = project.android.applicationVariants
        } else {
            variants = project.android.libraryVariants
        }

        project.dependencies {
            // TODO this should come transitively
            compile 'org.aspectj:aspectjrt:1.8.6'
        }

        project.extensions.create('hugo', HugoExtension)
        project.afterEvaluate{

        variants.all { variant ->
            log.error("Hugo compile")
            JavaCompile javaCompile = variant.javaCompile
            javaCompile.doLast {
                logger.error("do java compile last")
            }

            def name = project.name;
            log.error(name)
            log.error("project name "+javaCompile.name);
            String aopTask = "aop${variant.name}task"
//            def defaultTask = createDefaultTask(project,javaCompile,aopTask)
//            javaCompile.finalizedBy defaultTask


        }
    }
    }

    private DefaultTask createDefaultTask(Project project,JavaCompile javaCompile,String name){
        def defaultTask = project.tasks.create(name, DefaultTask);
        defaultTask.doLast {
            String[] args = [
                    "-showWeaveInfo",
                    "-1.8",
                    "-inpath", javaCompile.destinationDir.toString(),
                    "-aspectpath", javaCompile.classpath.asPath,
                    "-d", javaCompile.destinationDir.toString(),
                    "-classpath", javaCompile.classpath.asPath,
                    "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
            ]




            MessageHandler handler = new MessageHandler(true);
            def log = project.logger;
            log.error("start compile")
            File file = new File(javaCompile.destinationDir.toString());
            log.error("file "+(file.size())+(file.path))
            new Main().run(args, handler);
            for (IMessage message : handler.getMessages(null, true)) {
                switch (message.getKind()) {
                    case IMessage.ABORT:
                    case IMessage.ERROR:
                    case IMessage.FAIL:
                        log.error message.message, message.thrown
                        break;
                    case IMessage.WARNING:
                        log.warn message.message, message.thrown
                        break;
                    case IMessage.INFO:
                        log.info message.message, message.thrown
                        break;
                    case IMessage.DEBUG:
                        log.debug message.message, message.thrown
                        break;
                }
            }

        }
    }

}